<?php

	/**
	 * 领取会员卡前置操作
	 * @param $model
	 * 生成会员卡号
	 */
	function vip_add_before($model){
		
		//基础会员卡编号
		if(empty($model->vipid)){
			$model->vipid = $model->getBaseVipid();
		}
		
		//用户编号
		if(empty($model->userid)){
			$mobile = $model->mobile;
			$model->userid = getUseridByMobile($mobile);
		}
		
		//会员卡号
		$model->vipno = $model->createVipno($model->vipid);
		
		return $model;
	}
	
	/**
	 * 领取会员卡后置操作
	 * 增加用户会员卡:可使用积分和可使用余额
	 * @param $table 用户会员卡关联表
	 * @param $id    用户会员卡关联表主键
	 */
	function vip_add_after($table , $id){
		
		$module = M("$table");
		$view   = $module->find($id);
		
		$model = D("Actvipcard");
		$rows  = $model->find($view['vipid']);
		$init_jifen = $rows["init_jifen"];
		$init_money = $rows["init_money"];
		
		//初始化会员的可使用余额和可使用积分
		$view["use_jifen"]   += $init_jifen;
		$view["total_jifen"] += $init_jifen;
		$view["use_money"]   += $init_money;
		$module->save($view);
		
		//添加会员金额操作记录
		$map['addtime']   = time();
		$map['vipuserid'] = $view["id"];
		$map['userid']    = $view["userid"];
		$map['val']       = $init_money;
		$map['act']       = 1;
		$map['cascade']   = 2;
		$map['remark']    = L('aop_api_lqcard').":[".$rows['title']."],".L('aop_api_zscsje').":".$init_money;
	  //$map['addid']     = I("param.addid");
		$model->moneylog($map);
		
		//添加会员积分操作记录
		$map['val']       = $init_jifen;
		$map['remark']    = L('aop_api_lqcard').":[".$rows['title']."],".L('aop_api_zscsje').":".$init_jifen;
		$model->creditlog($map);
	}
	
	/**
	 * 添加对应会员卡金额操作记录前置置操作
	 * @param $model
	 * 判断余额是否足够
	 */
	function addmoney_add_before($model,$now){
		
		$act = $model->act;
		if($act == 2){
			
			$now_money = $model->val;
			$use_money = $model->getVipUseMoney($model->userid);
			
			if($now_money>$use_money){
				$now->error(L('aop_api_hyyebz'));
			}
		}
		
		return $model;
	}
	
	/**
	 * 添加对应会员卡金额操作记录后置操作
	 * @param $model
	 * 更新会员卡用户可用金额
	 * @param $act     操作类型(1充值 2使用[默认])
	 * @param $cascade 是否需要更新用户会员记录表的可使用余额(1需要[默认] 2不需要)
	 */
	function addmoney_add_after($table , $id){
		
		$module = M("$table");
		$rows   = $module->find($id);
		$model  = M("ActvipcardUser");
	
		$userid = $rows['userid'];
		$act    = $rows['act'];
		$money  = $rows['val'];
		$cascade= $rows['cascade'];
	
		$vipuid = $model->where(array("userid"=>$userid))->getField("id");
		$m['id']= $vipuid;
		
		if($cascade == 1){
			if($act == 1){
				//获得金额
				$model->where($m)->setInc("use_money",$money);
			}else{
				//消费金额
				$model->where($m)->setDec("use_money",$money);
			}
		}
		//增加使用次数
		$model->where($m)->setInc("usedtimes",1);
		$module->save(array("id"=>$id,"vipuserid"=>$vipuid));
	}
	
	/**
	 * 添加对应会员卡积分操作记录前置置操作
	 * @param $model
	 * 判断积分是否足够
	 */
	function addcredit_add_before($model,$now){
	
		$act = $model->act;
		if($act == 2){
				
			$now_credit = $model->val;
			$use_credit = $model->getVipUseCredit($model->userid);
				
			if($now_credit>$use_credit){
				$now->error(L('aop_api_hyjfbz'));
			}
		}
		return $model;
	}
	
	/**
	 * 添加对应会员卡积分操作记录后置操作
	 * @param $model
	 * 更新会员卡用户可用积分
	 */
	function addcredit_add_after($table , $id){
	
		$module = M("$table");
		$rows   = $module->find($id);
		$model  = M("ActvipcardUser");
	
		$userid = $rows['userid'];
		$act    = $rows['act'];
		$credit = $rows['val'];
		$cascade= $rows['cascade'];
	
		$vipuid = $model->where(array("userid"=>$userid))->getField("id");
		
		if($cascade == 1){
			$m['id'] = $vipuid;
			if($act == 1){
				$model->where($m)->setInc("use_jifen"  ,$credit);
				$model->where($m)->setInc("total_jifen",$credit);
			}else{
				$model->where($m)->setDec("use_jifen",$credit);
			}
			$model->where($m)->setInc("usedtimes",1);
		}

		$module->save(array("id"=>$id,"vipuserid"=>$vipuid));
	
	}
	
	
	/**
	 * 领取优惠卷前置操作
	 * @param $model
	 * 生成优惠卷编号
	 */
	function coupon_add_before($model){
	
		//优惠卷编号
		$yhjid = $model->yhjid;
		$model->yhjno = $model->createCouponno($yhjid);
		
		//用户编号
		if(empty($model->userid)){
			$mobile = $model->mobile;
			$model->userid = getUseridByMobile($mobile);
		}
		
		return $model;
	}
	
	/**
	 * 添加优惠卷使用记录前置操作
	 * 判断优惠卷是不是该用户拥有
	 */
	function yhjlog_add_before($model,$now){
		
		$userid   = $model->userid;
		$couponid = $model->couponid;
		$moule    = M("ActyhjCoupon");
		
		$row = $moule->find($couponid);
		if($userid != $row['userid']){
			$now->error(L("aop_api_nosyoyhj"));
		}
		return $model;
	}
	
	/**
	 * 添加优惠卷使用记录后置操作
	 * 更新优惠卷领取记录(usedtimes已使用次数  status使用状态)
	 * 直接返回优惠金额
	 */
	function yhjlog_add_after($table, $id){
		
		$module = M("$table");
		$rows   = $module->find($id);
		
		$model  = M("ActyhjCoupon");
		$m['id']= $rows['couponid'];
		$model->where($m)->setInc("usedtimes",1);

		//$m['val']     = calcCoupon($rows['couponid']);
		$m['usetime'] = time();
		$m['status']  = 1;
		$model->save($m);
		
	}
	
	/**
	 * 登记新会员后置操作
	 * 新增会员卡
	 */
	function custom_add_after($table, $id){
		
		$module   = M("$table");
		$model    = D("VipUser");
		$rows     = $module->find($id);
		$password = $_GET['password'];
		
		$map['company']  = $rows['company'];
		$map['outletid'] = $rows['outletid'];
		$map['mobile']   = $rows['mobile'];
		$map['email']    = $rows['email'];
		$map['vipno']    = $rows['vipno'];
		$map['cardid']   = $_GET['cardid'];
		$map['customid'] = $id;
		$map['addtime']  = time();
		$map['password'] = myencrypt($password);
		
		$id   = $model->addCard($map);
		$data = $model->find($id);
		
		if($data['use_jifen']>0){
			$model->addCredit($data,1,$data['use_jifen']);
		}
		if($data['use_money']>0){
			$model->addMoney($data,1,$data['use_money']);
		}
		  
	}
	
	function editprize_edit_before($model,$now){
		
	}

	